﻿using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XNA_Game_Engine.GUI
{
    public class GuiSlot
    {
        public Rectangle ThisObject { get; private set; }

        public Texture2D Sprite { get; private set; }
        public Texture2D Tooltip { get; private set; }
        public int LinePosition { get; private set; }
        public int ColumnPosition { get; private set; }
        public bool CanBeModified { get; private set; }
        public bool IsSelected { get; set; }

        private Rectangle _mouseas;

        private bool _showTooltip;
        private Rectangle _tooltipPosition;
        private bool _mouseClicked;
        private bool _mouseDoubleClicked;
        //private bool _nextClickCanBeDouble;

        private double _keepClickTime;
        private double _keepRightClickTime;
        //private double _keepDoubleClickTime;

        public Action Actions;
        public Action<GuiSlot> MouseClick;
        public Action<GuiSlot> MouseRightClick;
        public Action<GuiSlot> DoDragAction;
        //public Action<GuiSlot> MouseDoubleClick;

        public GuiSlotList Father;


        public GuiSlot(int w, int h, int linePosition, int columnPosition, Texture2D sprite, bool canBeModified, GuiSlotList father, Texture2D tooltip)
        {
            CanBeModified = canBeModified;

            IsSelected = false;

            LinePosition = linePosition;
            ColumnPosition = columnPosition;

            _keepClickTime = 0;
            _keepRightClickTime = 0;

            _mouseClicked = false;
            //_keepDoubleClickTime = 0;

            Sprite = sprite;
            Father = father;

            int x = w * ColumnPosition;
            int y = h * LinePosition;
            ThisObject = new Rectangle(x,y,w,h);

            MouseClick = null;
            MouseRightClick = null;

            DoDragAction = null;
            //MouseDoubleClick = null;

            _mouseas = StaticGui.MouseAsRectangle();

            Tooltip = tooltip;
            _showTooltip = false;

            if (Tooltip != null)
            {
                GetTooltipPosision();
            }
        }

        public GuiSlot(Rectangle obj, int columnPosition, int linePosition, Texture2D sprite, bool canBeModified, GuiSlotList father, Texture2D tooltip)
        {
            CanBeModified = canBeModified;

            IsSelected = false;

            ColumnPosition = columnPosition;
            LinePosition = linePosition;

            _keepClickTime = 0;
            _keepRightClickTime = 0;

            _mouseClicked = false;
            //_keepDoubleClickTime = 0;

            Sprite = sprite;
            Father = father;

            ThisObject = obj;

            MouseClick = null;
            MouseRightClick = null;

            DoDragAction = null;
            //MouseDoubleClick = null;

            _mouseas = StaticGui.MouseAsRectangle();

            Tooltip = tooltip;
            _showTooltip = false;

            if (Tooltip != null)
            {
                GetTooltipPosision();
            }
        }

        public void Update(GameTime gameTime)
        {
            bool mouseColision = StaticGui.MouseColision(CorrectRepresentationOfThisObjectGambiarraDaBraba());

            if (Tooltip != null && mouseColision && !StaticGui.IsDragging())
                _showTooltip = true;
            else
                _showTooltip = false;

            if (MouseClick != null && !StaticGui.IsMouseBeingUsed() && StaticGui.MouseState.LeftButton == ButtonState.Pressed 
                &&  mouseColision
                && StaticGui.CheckTime(gameTime, _keepClickTime))
            {
                _mouseas = StaticGui.MouseAsRectangle();

                Console.WriteLine("click");

                _mouseClicked = true;

                _keepClickTime = gameTime.TotalGameTime.TotalMilliseconds;

                //StaticGui.AppMouseState = StaticGui.AppMouseStates.Click;

                StaticGui.UsingMouse(true);
            }

            //if (_mouseDoubleClicked && StaticGui.MouseColision(CorrectRepresentationOfThisObjectGambiarraDaBraba()))
            //{
            //    if (MouseDoubleClick != null)
            //    {
            //        Console.WriteLine("double click");
            //        _mouseDoubleClicked = false;
            //        MouseDoubleClick(this);
            //        StaticGui.AppMouseState = StaticGui.AppMouseStates.None;
            //    }
            //}

            if (MouseRightClick != null && !StaticGui.IsMouseBeingUsed() && StaticGui.MouseState.RightButton == ButtonState.Pressed 
                && StaticGui.MouseColision(CorrectRepresentationOfThisObjectGambiarraDaBraba()) 
                && StaticGui.CheckTime(gameTime, _keepRightClickTime))
            {
                _keepRightClickTime = gameTime.TotalGameTime.TotalMilliseconds;
                MouseRightClick(this);
            }

            CheckDragAndMouseClick(gameTime);
        }

        public void Draw(SpriteBatch render)
        {
            var obj2 = CorrectRepresentationOfThisObjectGambiarraDaBraba();
           // render.Draw(Sprite, new Vector2(ThisObject.X, ThisObject.Y), Color.White);
            var targetRect = new Rectangle( 0, 0, Sprite.Width, Sprite.Height );
            render.Draw(Sprite, obj2, targetRect, Color.White);

            if(_showTooltip)
                render.Draw(Tooltip, _tooltipPosition, new Rectangle(0, 0, 768, 256), Color.White);

            if (IsSelected)
                render.Draw(StaticGui.Image[30], new Rectangle(obj2.X-1, obj2.Y-1, obj2.Width, obj2.Height),
                    new Rectangle(0, 0, StaticGui.Image[30].Width, StaticGui.Image[30].Height),
                    Color.White);
        }

        public void GetTooltipPosision()
        {
            var obj1 = CorrectRepresentationOfThisObjectGambiarraDaBraba();
            _tooltipPosition = new Rectangle(obj1.X + (obj1.Width / 2) - 62, obj1.Y - 50, 125, 50);
            Console.WriteLine("X " + _tooltipPosition.X + " Y " + _tooltipPosition.Y);
            //_tooltipPosition = new Rectangle(300, 300, 125, 50);
        }

        private void CheckDragAndMouseClick(GameTime gameTime)
        {
            if (!_mouseClicked)
                return;

            if (!StaticGui.IsDragging() && !_mouseas.Equals(StaticGui.MouseAsRectangle()) || 
                200 < gameTime.TotalGameTime.TotalMilliseconds - _keepClickTime )
            {
                StaticGui.StartDrag(this);
            }

            if (StaticGui.MouseState.LeftButton == ButtonState.Pressed)
                return;

            if (!StaticGui.IsDragging())
                MouseClick(this);

            StaticGui.StopDrag();
            StaticGui.UsingMouse(false);

            _mouseClicked = false;

            if (DoDragAction == null)
                return;

            DoDragAction(this);
        }

        public bool Colision(Rectangle r)
        {
            return CorrectRepresentationOfThisObjectGambiarraDaBraba().Intersects(r);
        }

        public Rectangle CorrectRepresentationOfThisObjectGambiarraDaBraba()
        {
            if (Father == null)
            {
                Console.WriteLine("Father is null... I am an orphan... =( : " + new StackTrace());
                return new Rectangle(0,0,0,0);
            }

            return new Rectangle(Father.ThisObject.X + ThisObject.X, Father.ThisObject.Y + ThisObject.Y, ThisObject.Width, ThisObject.Height);
        }

        public void ChangeToThis(GuiSlot slot)
        {
            Sprite = slot.Sprite;
            DoDragAction = slot.DoDragAction;
            MouseClick = slot.MouseClick;
            MouseRightClick = slot.MouseRightClick;
            //MouseDoubleClick = slot.MouseDoubleClick;
        }

        public void Clear()
        {
            Sprite = StaticGui.Image[6];
            DoDragAction = null;
            MouseClick = null;
            MouseRightClick = null;
            //MouseDoubleClick = null;
        }
    }
}
